/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Compresor;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;


/**
 *
 * @author asc
 */
public class Compresor {

    /**
     * @param args the command line arguments
     */
    private Huffman compresor;
    
    public static void main(String[] args) {
        
        if(args.length == 2)
        {
            Compresor a = new Compresor();
            if(args[0].equals("-c"))
            {
                a.crun(args[1]);
            }
            else if(args[0].equals("-d"))
            {
                a.drun(args[1]);
            }
        }
        /*
        String x = "10000000000101001000001000000000000100000000001";
        System.out.println(x);
        Lector.GuardaFichero("test", x);
        String y = Lector.LeerFichero("test");
        System.out.println(y);
        */
    }
    
    public void crun(String filePath)
    {
        compresor = new Huffman();
        String tmp = compresor.comprime(filePath);
        
        Lector.GuardaFichero(filePath+".egas.data", tmp);
        
        String[] x = filePath.split("/");
        //System.out.println(x.length);
        escritura(x[x.length-1]);
    }
    
    public void drun(String filePath)
    {
        lectura(filePath);
        System.out.println("DATOS");
        String x=Lector.LeerFichero(filePath+".data");
        System.out.println("RESTO");
        compresor.descomprime(x);
    }
  
    public void lectura(String ent){


        FileInputStream fis = null;
        ObjectInputStream in = null;
        try
        {
            fis = new FileInputStream(ent);
            in = new ObjectInputStream(fis);
            compresor = (Huffman)in.readObject();
        } catch(IOException ioe)
        {
            System.err.println("Error de entrada/salida");
        }catch (ClassNotFoundException cnfe)
        {
            System.err.println("Error de Clase no encontrada");
        }
    }

    public void escritura(String salida){

        FileOutputStream fos=null;
        ObjectOutputStream oos=null;
        try {
            fos = new FileOutputStream(salida+".egas");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(compresor);
        }
        catch (Exception e) {
            System.out.println("No se pudo guardar el fichero con el resultado");
            System.out.println(e);
        }
        finally {
            try {
                if (oos != null && fos != null) {
                    oos.close();
                    fos.close();
                }
            }
            catch (Exception e) {System.out.println(e);}
        }
    }

    public static void TestYPruebas()
    {
        // TODO test y pruebas
        
        BitSetUtil a = new BitSetUtil();
        BitSetUtil b = new BitSetUtil(3);

        a.set(true);
        a.set(false);
        a.set(false);
        a.set(false);

        System.out.println(a.toString());
        System.out.println("BBBBBBB");

        b.set(true);
        b.set(false);
        b.set(false);
        b.set(false);

        BitSetUtil c = (BitSetUtil)a.clone();

        System.out.println(b.toString());
        System.out.println(a.equals(b));
        System.out.println(a.equals(c));


        LinkedList<Integer> lista = new LinkedList<Integer>();
        for(int i=0;i<10;i++)
        {
            lista.add(i);
        }
        Collections.sort(lista);

        for(ListIterator it = lista.listIterator(lista.size()); it.hasPrevious();)
        {
            final Object elementoLista = it.previous();
            System.out.println(elementoLista.toString());
        }
        
        LexicalAnalisis pruebaDefinitiva = new LexicalAnalisis("/home/asc/Documentos/lsusb.txt");
        pruebaDefinitiva.AnalizeInput1Char();

        Diccionario nuevo = new Diccionario();
        nuevo.Huffman(pruebaDefinitiva);

    }
}
